package com.mynovamobile.android;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONException;
import org.json.JSONObject;

import com.mynovamobile.android.R;
import com.mynovamobile.android.R.id;
import com.mynovamobile.android.R.layout;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.EditText;

public class LDAPLogin extends Activity {
	private final static String TAG = "[LDAPLogin]";
	private MNMRequest loginData;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		Log.v(TAG, "Called onCreate");
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ldap_login);
		loginData = new MNMRequest(this);
		final MNMUserDefaults userData = MNMUserDefaults.getInstance(this);

		final LDAPLogin ldapContext = this;
		final EditText usernameField = (EditText) findViewById(R.id.ldap_login_field_user);
		final EditText passwordField = (EditText) findViewById(R.id.ldap_login_field_pass);
		ImageButton b = (ImageButton) findViewById(R.id.ldap_login_loginButton);
		b.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Log.v(TAG, "Button Clicked");
				String username = usernameField.getText().toString();
				String password = passwordField.getText().toString();
				if (username.length() > 0 && password.length() > 0) {
					loginData.addData("username", username);
					loginData.addData("password", password);
					
					String url = getString(R.string.protocol) + getString(R.string.hostname) + getString(R.string.ldap_auth_url);
					String authenticationStatus;
					try {
						JSONObject responseData = loginData.executeRequest(url);
						authenticationStatus = responseData.getString("response_type");
					} catch (ClientProtocolException e) {
						Log.e(TAG, "Encountered: " + e.toString());
						new MNMMessage(ldapContext, "Request Error", e.getMessage(), MNMMessage.ERROR);
						return;
					} catch (UnsupportedEncodingException e) {
						Log.e(TAG, "Encountered: " + e.toString());
						new MNMMessage(ldapContext, "Invalid Encoding", e.getMessage(), MNMMessage.ERROR);
						return;
					} catch (IOException e) {
						Log.e(TAG, "Encountered: " + e.toString());
						new MNMMessage(ldapContext, "Network I/O Error", e.getMessage(), MNMMessage.ERROR);
						return;
					} catch (JSONException e) {
						Log.e(TAG, "Encountered: " + e.toString());
						new MNMMessage(ldapContext, "Parse Error", e.getMessage(), MNMMessage.ERROR);
						return;
					}
					if (authenticationStatus.equals("success")) {
						Log.i(TAG, "User succeeded LDAP authentication");
						userData.setLDAPUsername(username).setLDAPPassword(password);
						finish();
					} else {
						Log.w(TAG, "User failed LDAP authentication.");
						new MNMMessage(ldapContext, "Not Authenticated", "Your Villanova LDAP credentials could not be authenticated.", MNMMessage.ERROR);
					}
				} else {
					new MNMMessage(ldapContext, 
							"Invalid Username or Password", 
							"The Username or Password field was left blank.", MNMMessage.ERROR);
				}
			}
		});
	}
}